using Domain.Dto;
using Domain.Interface;
using Domain.Model;
using Gmmy.DomainExtension;
using Gmmy.RepositoryExtension;
using Gmmy.Utility;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Gmmy.Utility.Extensions;

namespace Infrastructure.Repository.Repository.Sql
{
    public class SampleModelRepository : RepositoryBase<SampleModel>, ISampleModelRepository
    {
        public SampleModelRepository(IDataBaseFactory<EfContext> databaseFactory)
            : base(databaseFactory)
        {

        }

		/// <summary>
        /// 获取案例模型列表
        /// </summary>
        /// <param name="input"></param>
        /// <param name="specification"></param>
        /// <returns></returns>
        public async Task<EntityList<SampleModelListDto>> GetList(SampleModelListInput input,
            ISpecification<SampleModelListInput> specification)
        {
            var result = new EntityList<SampleModelListDto>();
               var query = (from a in dbset
                select new SampleModelListInput()
                {
                    ID = a.ID,
                    Title = a.Title,
                    CreateTime = a.CreateTime,
                    IsDeleted = a.IsDeleted,

                }).Where(specification.SatisfiedBy());
            if (input.OrderParm == null || !input.OrderParm.Any())
            {
                input.OrderParm = new Dictionary<string, string> {{ "CreateTime", "DESC"}};
            }
            query = DataSort(query, input.OrderParm);
            var data = await FindAllAsyncAsQuery(query, input.PageIndex, input.PageSize);
            if (data.Data.Any())
            {
                result = data.MapTo<EntityList<SampleModelListDto>, EntityList<SampleModelListInput>>();
            }
            return result;
        }
    }
}